package com.aquila.bible.views;

import com.aquila.bible.R;
import com.aquila.bible.system.Utils;
import com.aquila.bible.utils.BookText;

import android.R.integer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class BookTextView extends View {
	/** 文本间距--英文 */
	private static int TEXT_SPACE_BETWEEN_EN = 0 ;
	/** 文本间距--中文 */
	private static int TEXT_SPACE_BETWEEN_CN = 0 ;
	
	/** 屏幕的高度和宽度 */
	private int screenWidth, screenHeight ;
	/** 显示文本区域的宽和高 */
	private int contentWidth, contentHeight ;
	/** 上下左右边距 */
	private int marginVertivcal = 20, marginHorizontal = 15 ;
	
	/** 每页显示的文字数 */
	private int pageWords ;
	/**总页数**/
	private int pageSize ;
	/** 当前页的索引 */
	private int pageIndex ;
	/**文件的字符数*/
	private int contentWordsLength ;
	/** 文字大小 */
	private int textSize = 26 ;
	
	/**每页显示的行数*/
	private int lines ;
	/**行间距*/
	private int lineSpace = 5;
	/**文字间距*/
	private int textSpace = 2;
	/**显示的文字颜色*/
	private int textColor = 0xff000000;
	private Context context ;
	private Paint mPaint ;
	/**文本*/
	private String contentString ;
	public BookTextView(Context context) {
		super(context);
		this.context = context ;
		screenWidth = Utils.getDeviceScreenWidth(context) ;
		screenHeight = Utils.getDeviceScreenHeight(context) ;
		
		contentHeight = screenHeight - marginVertivcal * 2 ;
		contentWidth = screenWidth - marginHorizontal * 2 ;
		//计算可以显示多少行
		lines = contentHeight / (textSize + lineSpace) ;

		//计算此屏幕现实的文字数目
		pageWords = (lines * (contentWidth / (textSize + textSpace))) ;
 		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;
		mPaint.setTextSize(textSize) ;
		mPaint.setColor(textColor) ;
		setBackgroundResource(R.drawable.readbg_00) ;
		
	}
	
	/***
	 * 
	 *2012-7-27 上午11:21:55
	 */
	public void prepareData(){
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			invalidate() ;
		}
		
		
		return super.onTouchEvent(event);
	
	}
	
	
//	String displayString ;
	int index = 0 ;
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		displayString = contentString.substring(0, pageWords) ;
		int currLine = 0 ;
		int pos_x = marginHorizontal ;
		boolean isFinish = false ;
		int pos_y = marginVertivcal + lineSpace ;
		while(!isFinish){
			if (contentString != null) {
				if (index < contentString.length()) {
					char c = contentString.charAt(index++) ;
					if ('\n' == c) {
						currLine++ ;
						pos_x = marginHorizontal ;
						pos_y += textSize + lineSpace ;
					}
					else if (c >= 0 && c <= 122){
						canvas.drawText(c+"", pos_x, pos_y, mPaint) ;
						pos_x += textSize/2 + TEXT_SPACE_BETWEEN_EN ;
					}
					else { // 中文
						canvas.drawText(c+"", pos_x, pos_y, mPaint) ;
						pos_x += textSize + TEXT_SPACE_BETWEEN_CN ;
					}
					
					if (pos_x >= screenWidth - marginHorizontal*2) { // 当前行显示完后换行显示
						currLine++ ;
						pos_x = marginHorizontal ;
						pos_y += textSize + lineSpace ;
					}
					
					if (pos_y >= screenHeight - marginVertivcal) { //本页显示完
						isFinish = true ;
					}
				}//结束if (index < contentString.length()
			} // 结束if (contentString != null)
		}// 结束while循环
	
	
	}


	public int getContentWordsLength() {
		return contentWordsLength;
	}


	public void setContentWordsLength(int contentWordsLength) {
		this.contentWordsLength = contentWordsLength;
		pageSize = (contentWordsLength / pageWords) + 1 ;
	}

	public String getContentString() {
		return contentString;
	}

	public void setContentString(String contentString) {
		this.contentString = contentString;
		contentWordsLength = contentString.length() ;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
